package com.zhuozhengsoft.springbootpageoffice.controller;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import com.zhuozhengsoft.pageoffice.wordwriter.WordDocument;
import com.zhuozhengsoft.springbootpageoffice.entity.Doc;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Administrator
 */
@RestController
public class IndexController {
    private  String docPath = "D:\\pageoffice\\doc";
    private List<Doc> list = new ArrayList();

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView showIndex(Map<String, Object> map) throws IOException {
        ModelAndView mv = new ModelAndView("index");

        list = getFileList();
        map.put("list", list);

        return mv;
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView showMain(Map<String, Object> map) throws IOException {
        ModelAndView mv = new ModelAndView("index");

        list = getFileList();
        map.put("list", list);

        return mv;
    }

    @RequestMapping(value = "Word", method = RequestMethod.GET)
    public ModelAndView showWord(HttpServletRequest request, Map<String, Object> map) {
        int id = Integer.parseInt(request.getParameter("id"));
        Doc doc = list.get(id - 1);
//        System.out.println("showWord|" + id + "|" + doc.getFileName());

        WordDocument wordDoc=new WordDocument();
        wordDoc.setDisableWindowSelection(true);//禁止选中
        wordDoc.setDisableWindowRightClick(true);//禁止右键
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
        poCtrl.setServerPage(request.getContextPath() + "/poserver.zz");//设置服务页面
        poCtrl.setMenubar(false);//隐藏菜单栏
        poCtrl.setOfficeToolbars(false);//隐藏Office工具条
        poCtrl.setCustomToolbar(true);//隐藏自定义工具栏
        poCtrl.setJsFunction_AfterDocumentOpened("AfterDocumentOpened");

        poCtrl.addCustomToolButton("目录", "onLeft", 12);
        poCtrl.addCustomToolButton("关闭", "onClose", 21);
        //设置页面的显示标题
        poCtrl.setCaption(doc.getFileName());
        poCtrl.setWriter(wordDoc);//注意：使用WordDocument类后，此句必须
        //打开Word文档
        poCtrl.setProtectPassword("123123");
        poCtrl.webOpen(doc.getSubject(), OpenModeType.docReadOnly, "张三");
        map.put("pageoffice", poCtrl.getHtmlCode("PageOfficeCtrl1"));
        ModelAndView mv = new ModelAndView("ReadOnly/Word");
        return mv;
    }

    private List<Doc> getFileList() throws IOException {
        List<Doc> fileList = new ArrayList();
        Path initPath = Paths.get(docPath);
        Files.walkFileTree(initPath, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                    throws IOException {
                String fileName = file.getFileName().toString();
                if(!file.toFile().isHidden() && (fileName.endsWith(".doc") || fileName.endsWith(".docx") || fileName.endsWith(".txt"))) {
                    int index = fileName.lastIndexOf(".");
                    String name = fileName.substring(0, index);
                    String suffix = fileName.substring(index);
//                    System.out.println(fileName + "|" + name + "|" + file.toFile().isHidden());
                    Doc doc = new Doc();
                    doc.setId(fileList.size() + 1);
                    doc.setFileName(name);
                    doc.setSubject(file.normalize().toString());
                    doc.setSubmitTime(suffix);
                    fileList.add(doc);
                }
                return FileVisitResult.CONTINUE;
            }
        });
        return fileList;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView showList(Map<String, Object> map) throws IOException {
        ModelAndView mv = new ModelAndView("list");
        return mv;
    }
}
